package com.gong;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.omg.CosNaming.NamingContextExtPackage.StringNameHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import groovy.transform.Field;

@Controller
public class FileUploadController
{
	@RequestMapping("/file")
	public String file() {
		return "file";
	}
	
	@RequestMapping("/upload")
	@ResponseBody
	public String fileUpload(@RequestParam("filename") MultipartFile file) {
		if(file.isEmpty()) {
			return "false";
		}
		
		String fileName = file.getOriginalFilename();
		System.out.println("++++++++++FileName:"+fileName);
		
		String filePath = "D:/test/";
		File dest  = new File(filePath + fileName);
		if(!dest.getParentFile().exists()) {
			dest.getParentFile().mkdirs();
		}
		
		try
		{
			file.transferTo(dest);
			return "true";
		} catch (Exception e)
		{
			// TODO: handle exception
			e.printStackTrace();
		}
		return "";
	}
	
	@RequestMapping("/multiFile")
	public String multiFile() {
		return "multiFile";
	}
	
	@RequestMapping(value = "/multi/upload",method = RequestMethod.POST)
	public @ResponseBody String multiFileUpload(HttpServletRequest request) throws IllegalStateException, IOException {
		List<MultipartFile> files =  ((MultipartHttpServletRequest)request).getFiles("filename");
		MultipartFile file = null;
		String filePath = "D:/test/";
		File dest  = null;
		for(MultipartFile file1:files) {
			if(file1.isEmpty()) {
				return "false";
			}
			dest = new File(filePath + file1.getOriginalFilename());
			file1.transferTo(dest);
		}
		
		return "true";
		
	}
	
	@RequestMapping("/download")
	public String downloadFile(HttpServletResponse response) throws IOException {
		String fileName = "1.jpg";
		String filePath = "D:/test/";
		File file = new File(filePath+fileName);
		if(file.exists()) {
			response.setContentType("application/force-download");
			response.addHeader("Content-Disposition","attchment;filename="+fileName);
			
		}
		
		byte[] buffer = new byte[1024];
		FileInputStream fis = null;
		OutputStream os = null;
		BufferedInputStream bis = null;
		
		fis = new FileInputStream(file);
		bis = new BufferedInputStream(fis); 
		
		os = response.getOutputStream();
		int i = bis.read(buffer);
		
		while(i!=-1) {
			os.write(buffer);	
			i = bis.read(buffer);
		}
		
		bis.close();
		fis.close();
		System.out.println("+++++++++++download OK!");
		return null;
		
	}
}
